package com.lzs.kunei.controller;


import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.entity.*;
import com.lzs.kucun.service.StockService;
import com.lzs.kunei.service.AllocationDetailService;
import com.lzs.kunei.service.AllocationService;
import com.lzs.util.JwtUtils;
import com.lzs.util.R;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wpy
 * @since 2020-11-24
 */
@RestController
@ApiModel(value = "调拨明细")
@RequestMapping("/kunei/allocationdetail")
public class AllocationDetailController {
    @Autowired
    private AllocationDetailService allocationDetailService;
    @Autowired
    private StockService stockService;
    @Autowired
    private AllocationService allocationService;

    @ApiOperation(value = "添加调拨明细")
    @PostMapping("/addAllocationDetail")
    public R addAllocationDetail(@RequestBody AllocationDetail allocationDetail, HttpServletRequest request){
        try {
            String token=request.getHeader("token");
            Long userid= JwtUtils.getMemberIdByJwtToken(token);
            User user=stockService.selectUser(userid);
            String name = user.getName();
            String allocationno = allocationDetail.getAllocationno();
            QueryWrapper<Allocation> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("allocationno",allocationno);
            Allocation allocation = allocationService.list(queryWrapper).get(0);
            Long bwarehouseid = allocation.getBwarehouseid();
            System.out.println(allocationDetail.toString());
            String commoditybar = allocationDetail.getCommoditybar();
            String locatorcode = allocationDetail.getLocatorcode();
            Integer qty = allocationDetail.getQty();
            R r = stockService.updateUqtyQuantity(locatorcode, commoditybar, bwarehouseid, qty);
            if (r.getSuccess()){
                Boolean aBoolean = allocationDetailService.comIsExist(allocationno, locatorcode, commoditybar, qty);
                if (!aBoolean){
                    Commodity commodity = stockService.selectCommodity(commoditybar);
                    allocationDetail.setCommodityid(commodity.getId());
                    allocationDetail.setCommodityname(commodity.getName());
                    allocationDetail.setCreateid(userid);
                    allocationDetail.setCreator(name);
                    allocationDetail.setUpdateid(userid);
                    allocationDetail.setUpdator(name);
                    allocationDetailService.save(allocationDetail);}
                return R.ok().message("添加成功");
            }else {
                return R.error().message("数量不足，添加失败");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            return R.error().message("添加失败");
        }


    }
    @ApiOperation(value = "查询调拨明细")
    @PostMapping("/selectAllocationDetail/{current}/{size}/{allocationno}")
    public R selectAllocationDetail(@PathVariable Integer current, @PathVariable Integer size,@PathVariable String allocationno){
        try {
            QueryWrapper<AllocationDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("allocationno", allocationno);
            Page<AllocationDetail> page=new Page<>(current,size);
            allocationDetailService.page(page,queryWrapper);
            return R.ok().message("查询成功").data("allocationDetails", page);
        }catch (RuntimeException e){
            e.printStackTrace();
            return R.error().message("查询失败").data("allocationDetails", null);
        }
    }
    @ApiOperation(value = "添加调拨明细下拉")
    @PostMapping("/selectAllocationDetailByNo")
    public R selectAllocationDetail2( @RequestBody AllocationDetail allocationDetail){
        String allocationno = allocationDetail.getAllocationno();
        String locatorcode = allocationDetail.getLocatorcode();
        QueryWrapper<Allocation> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("allocationno",allocationno);
        Allocation allocation = allocationService.list(queryWrapper).get(0);
        Long bwarehouseid = allocation.getBwarehouseid();
        QueryWrapper<Stock> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("warehouseid",bwarehouseid);
        List<Stock> list = stockService.list(queryWrapper1);
        Set set=new HashSet();
        for (int i = 0; i <list.size() ; i++) {
            set.add(list.get(i).getLocatorcode());
        }
        if (StringUtils.isNotEmpty(locatorcode)) {
            queryWrapper1.eq("locatorcode", locatorcode);
        }
        List<Stock> list1=stockService.list(queryWrapper1);
        return R.ok().data("locatorcodes",set).data("commoditybars",list1);
    }


}

